/*
 * @Description: 通过channel互换参数  实现乒乓球原理
 * @Version: 3.0
 * @Autor: biao.hu
 * @Date: 2021-10-18 19:23:45
 * @LastEditors: biao.hu
 * @LastEditTime: 2021-10-18 19:25:16
 */
package channel

import (
	"fmt"
	"time"
)

type Ball struct {
	Hits int
}

func PassBall() {
	table := make(chan *Ball)
	go Player("ping", table)
	go Player("pong", table)

	table <- new(Ball)
	time.Sleep(10 * time.Second)
	<-table
}

func Player(name string, table chan *Ball) {
	for {
		ball, ok := <-table
		if !ok {
			return
		}
		ball.Hits++
		fmt.Println(name, ball.Hits)
		time.Sleep(100 * time.Millisecond)
		table <- ball
	}
}
